package com.yin.myproject.arithmetic;

import java.util.Arrays;

/**
 * @author Eason
 * @create 2018-04-20 14:27
 **/
public class BubbleSort {
    public static void main(String[] args) {
//        int[] array = {94, 12, 34, 76, 26, 9, 0, 37, 55, 76, 37, 5, 68, 83, 90, 37, 12, 65, 76, 49};
        int[] array = {0, 5, 9, 12, 12, 26, 34, 37, 37, 37, 49, 55, 65, 68, 76, 76, 76, 83, 90, 94};
        BubbleSort sorter = new BubbleSort();
        int[] sort = sorter.sort(array);
        System.out.println(Arrays.toString(sort));
    }

    private int[] sort(int[] array) {
        boolean flag = true;
        int temp;
        int length = array.length;
        for (int i = 0; i < length; ++i) {
            for (int j = 1; j < length - i; j++) {
                if (array[j - 1] > array[j]) {
                    temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
        return array;
    }
}
